package com.example.user.myfirstapp.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.example.user.myfirstapp.R;
import com.example.user.myfirstapp.entity.Comment;
import com.example.user.myfirstapp.entity.GoodsType;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by user on 2016/6/30.
 */
public class GoodsCommentActivity extends Activity implements View.OnClickListener {
    private Button bt_comment_return;
    private Intent intent;
    private Gson gson;
    private List<Comment> commentLists;
    private ArrayList<HashMap<String, Object>> listItem;
    private ListView lv_commentlist;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comment);

        bt_comment_return=(Button)findViewById(R.id.bt_comment_return);
        bt_comment_return.setOnClickListener(this);
        lv_commentlist=(ListView)findViewById(R.id.lv_commentlist);

        intent =getIntent();
        String json =intent.getStringExtra("commentJson");
        if (!TextUtils.isEmpty(json)){
            loadcomment(json);
        }
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_comment_return:
                this.finish();
                //从左往右切换
                this.overridePendingTransition(0,R.animator.slide_right_out);
                break;
            default:
                break;
        }
    }

    private void loadcomment(String json){
        listItem=new ArrayList<HashMap<String, Object>>();
        gson = new Gson();
        commentLists=new ArrayList<Comment>();
        commentLists = gson.fromJson(json, new TypeToken<List<Comment>>(){}.getType());
        if(commentLists!=null&&commentLists.size()>0) {
            for (Comment item : commentLists) {
                HashMap<String, Object> map = new HashMap<String, Object>();
                map.put("Time", item.getDisplayTime());
                map.put("UserName", item.getCommentUserName());
                map.put("Content", item.getCommentContent());
                listItem.add(map);
            }
            //生成适配器的Item和动态数组对应的元素
            SimpleAdapter listItemAdapter = new SimpleAdapter(
                    this,
                    listItem, //数据源
                    R.layout.commentlist_item, //ListItem的XML实现
                    new String[]{"Time","UserName","Content"},//动态数组与listItem对应的子项
                    new int[]{R.id.tv_comment_time,R.id.tv_comment_user,R.id.tv_comment_content}
            );
            //添加并且显示
            lv_commentlist.setAdapter(listItemAdapter);
        }
    }
}
